import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class Banner1Wiget extends StatefulWidget {
  final List<String> list;
  final int autoplayDelay;
  Banner1Wiget({@required this.list, this.autoplayDelay = 3000});
  @override
  _Banner1WigetState createState() => _Banner1WigetState();
}

class _Banner1WigetState extends State<Banner1Wiget> {
  int _total;
  int _current = 1;

  @override
  void initState() {
    super.initState();
    _total = widget.list.length;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: 200,
      child: Stack(
        children: [
          // 第一层 构建banner
          buildBanner(),
          // 第二层 构建dot
          buildDot(),
        ],
      ),
    );
  }

  Widget buildDot() {
    return Positioned(
      bottom: 20,
      right: 20,
      child: Container(
        alignment: Alignment.center,
        height: 20,
        width: 40,
        decoration: BoxDecoration(
          color: Colors.grey.withOpacity(0.5),
          borderRadius: BorderRadius.all(Radius.circular(10)),
        ),
        child: Text("$_current/$_total", style: TextStyle(color: Colors.white)),
      ),
    );
  }

  buildBanner() {
    return Positioned.fill(
      child: Swiper(
        onIndexChanged: (value) {
          setState(() {
            _current = value + 1;
          });
        },
        curve: Curves.linear,
        autoplay: true,
        autoplayDelay: widget.autoplayDelay,
        itemCount: widget.list.length,
        itemBuilder: (BuildContext context, int index) {
          String banner = widget.list[index];
          return Image.asset(banner, fit: BoxFit.fill);
        },
      ),
    );
  }
}
